package com.hugoo.core.db.pools;

import com.hugoo.core.Conf;
import com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Lovelock.Chan
 */
public final class LxSqlServerPool implements NiCP {

    private final static Log log = LogFactory.getLog(LxSqlServerPool.class);
    MCPM mcpm = null;
    private final String inst;

    public LxSqlServerPool(String inst) {
        this.inst = inst;
        init();
    }

    @Override
    public void init() {
        SQLServerConnectionPoolDataSource dataSource = new SQLServerConnectionPoolDataSource();

        log.info("uri:" + Conf.getBaseProp(inst, "database.uri") + "\n"
                + "userName:" + Conf.getBaseProp(inst, "database.userName"));
        dataSource.setURL(Conf.getBaseProp(inst, "database.uri"));
        dataSource.setUser(Conf.getBaseProp(inst, "database.userName"));
        dataSource.setPassword(Conf.getBaseProp(inst, "database.password"));
        mcpm = new MCPM(dataSource, 30);//设置最大并发连接数为30
    }

    @Override
    public Connection getConn() {
        try {
            return mcpm.getConnection();
        } catch (SQLException ex) {
            log.error("打开数据库连接失败！", ex);
        } catch (Exception ex) {
            log.error("打开数据库连接出现异常！", ex);
        }
        return null;
    }
}
